var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    stepIndex: 0,
    password: '',
    sumsets: ['50元', '100元', '150元', '200元'],
    sumIndex: 0,
    freeOpen: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  forgetPwd: function () {
    var phone = app.globalData.personInfo.MOBILE;
    var msg = '发送（临时密码）到您的激活手机号码：' + phone;
    var that = this;
    wx.showModal({
      title: '',
      content: msg,
      confirmColor: '#a08257',
      confirmText: '短信验证',
      cancelText: '关闭',
      success: function (res) {
        if (res.confirm) {
          console.log('send MSG');
          that.sendMsgAction();
        }
      }
    })
  },
  sendMsgAction: function () {
    var phone = app.globalData.personInfo.MOBILE;
    var userid = app.globalData.userid;
    var url = '/yuanmai_we/user/password/forgot';
    var property = {
      "USERID": userid,
      "MOBILE": phone
    };
    var params = {
      __zaofans: true,
      datas: JSON.stringify(property)
    };
    app.loadDataFromServer(url, this.sendMsgActionCallBack, params, 'post', true);
  },
  sendMsgActionCallBack: function (res) {
    console.log(res);
    if (res.success) {
      wx.showToast({
        title: '发送成功',
      });
    }
  },
  goBack: function () {
    wx.navigateBack({});
  },
  // 确定免密支付
  confirmFreePay: function () {
    if (this.data.password == '') {
      return;
    }
    var url = '/yuanmai_we/user/password/verify';
    var userid = app.globalData.userid;
    var property = {
      "USERID": userid,
      "PASSWORD": this.data.password
    };
    var params = {
      __zaofans: true,
      datas: JSON.stringify(property)
    };
    app.loadDataFromServer(url, this.confirmFreePayCallBack, params, 'post', true);
  },
  confirmFreePayCallBack: function (res) {
    console.log(res);
    if (res.success) {
      this.getBalanceSum();
    }
    else {
      wx.showToast({
        title: res.errmsg,
        icon: 'none'
      });
    }
  },//----END------
  // 获取免密金额
  getBalanceSum: function () {
    wx.showLoading({
      title: '',
    });
    var url = '/yuanmai_we/user/balance/setting/get';
    app.loadDataFromServer(url, this.getBalanceSumCallBack, {}, 'get', true);
  },
  getBalanceSumCallBack: function (res) {
    wx.hideLoading();
    console.log(res);
    var freeOpen = res.freeSecret == 1 ? true : false;
    this.setData({
      stepIndex: 1,
      freeAmount: res.freeAmount,
      freeOpen: freeOpen
    });
  },//----END------
  // 提交设置免密支付
  submitPayFree: function () {
    var freeSecret = this.data.freeOpen == true ? 1 : 0;
    var property = {
      "freeSecret": freeSecret,
      "freeAmount": this.data.freeAmount
    };
    var params = {
      __zaofans: true,
      datas: JSON.stringify(property)
    };
    var url = '/yuanmai_we/user/balance/setting'
    app.loadDataFromServer(url, this.submitPayFreeCallBack, params, 'post', true)
  },
  submitPayFreeCallBack: function (res) {
    console.log(res);
    if (res.status) {
      wx.showToast({
        title: '设置成功',
        duration: 1000
      });
      var delayid = setTimeout(function () {
        clearTimeout(delayid);
        wx.navigateBack({});
      }, 1000);
    }
  },// ------END-------
  bindSumChange: function (e) {
    this.setData({
      sumIndex: e.detail.value
    });
  },
  switchChange: function (e) {
    this.setData({
      freeOpen: e.detail.value
    });
  },
  passwordInput: function (e) {
    this.setData({
      password: e.detail.value
    });
  }
})